package com.github.codemperor.core.message.dingtalk.robot;

import com.github.codemperor.core.message.dingtalk.robot.base.BaseDingRobot;
import com.github.codemperor.core.message.dingtalk.robot.bean.MarkdownMsgRequest;
import com.github.codemperor.core.message.dingtalk.robot.bean.RobotTextMsgRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 钉钉机器人markdown消息发送
 */
public class RobotMarkdownSend extends BaseDingRobot {
    private List<String> content = new ArrayList<>();
    private String title;
    private boolean isAtAll = false; //是否at所有人，默认否
    private String[] mobs; //需要at的人的手机集合

    private RobotMarkdownSend() {
    }

    public static RobotMarkdownSend build() {
        return new RobotMarkdownSend();
    }

    //将文本变为引用文本
    public RobotMarkdownSend reference(String... texts) {
        for (String text : texts) {
            content.add("> " + text);
        }
        return this;
    }

    //有序列表
    public RobotMarkdownSend orderList(String... text) {
        for (int i = 0; i < text.length; i++) {
            content.add((i + 1) + "." + text[i]);
        }
        return this;
    }

    //无序列表
    public RobotMarkdownSend unorderList(String... text) {
        for (String s : text) {
            content.add("- " + s);
        }
        return this;
    }

    //图片
    public RobotMarkdownSend img(String... urls) {
        for (String url : urls) {
            content.add("![screenshot](" + url + ")");
        }
        return this;
    }

    //粗体
    public RobotMarkdownSend B(String... texts) {
        for (String text : texts) {
            content.add("**" + text + "**");
        }
        return this;
    }

    //斜体
    public RobotMarkdownSend I(String... texts) {
        for (String text : texts) {
            content.add("*" + text + "*");
        }
        return this;
    }

    //h标题
    public RobotMarkdownSend h(String... h) {
        if (h.length > 0) { //1级标题
            content.add("# " + h[0]);
        }
        if (h.length > 1) { //2级标题
            content.add("## " + h[1]);
        }
        if (h.length > 2) { //3级标题
            content.add("### " + h[2]);
        }
        if (h.length > 3) { //4级标题
            content.add("#### " + h[3]);
        }
        if (h.length > 4) { //5级标题
            content.add("##### " + h[4]);
        }
        if (h.length > 5) { //6级标题
            content.add("###### " + h[5]);
        }
        return this;
    }

    //普通文本
    public RobotMarkdownSend text(String... texts) {
        content.addAll(Arrays.asList(texts));
        return this;
    }

    //推送消息的标题
    public RobotMarkdownSend title(String title) {
        this.title = title;
        return this;
    }

    //是否at所有人
    public RobotMarkdownSend atAll() {
        this.isAtAll = true;
        return this;
    }

    //at人列表
    public RobotMarkdownSend at(String... mobs) {
        this.mobs = mobs;
        return this;
    }

    @Override
    protected Object packageRequest(String title, List<String> rowContent, boolean isAtAll, String... mobs) {
        RobotTextMsgRequest.At at = new RobotTextMsgRequest.At();
        at.setAtAll(isAtAll);
        if (null != mobs && mobs.length > 0) {
            at.setAtMobiles(Arrays.asList(mobs));
        }

        MarkdownMsgRequest.Markdown markdown = new MarkdownMsgRequest.Markdown();
        markdown.setTitle(title);
        markdown.setText(String.join("\n\r", rowContent));

        MarkdownMsgRequest request = new MarkdownMsgRequest();
        request.setAt(at);
        request.setMarkdown(markdown);
        request.setMsgtype("markdown");
        return request;
    }

    @Override
    protected Object getMsgRequest() {
        return packageRequest(title, content, isAtAll, mobs);
    }

    public static void main(String[] args) {
//        RobotMarkdownSend.build().text("测试普通文本1", "测试普通文本2").title("测试title").atAll().send();
//        RobotMarkdownSend.build().text("测试at张璐").title("at张璐").at("18236995594", "13371652659").send();
//        RobotMarkdownSend.build()
//                .title("测试所有文本类型")
//                .h("1级标题", "2级标题", "3级标题", "4级标题", "5级标题", "6级标题")
//                .I("斜体")
//                .B("粗体")
//                .reference("引用文本")
//                .orderList("有序列表1", "有序列表2")
//                .unorderList("无序列表1", "无序列表2")
//                .img("https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png", "https://uptradeit.com/static/media/homeBanner-img.0e007e58.png")
//                .send();
//        RobotMarkdownSend.build().title("qa: Refund Paypal failed")
//                .B("qa: Refund Paypal failed")
//                .text("orderNo: aaaaa")
//                .text("chargeId: bbbbb")
//                .text("amount: 1000000.00")
//                .text("operatorName: 霞姐")
//                .text("operatorEmail: 霞姐@qq.com")
//                .send();
    }
}
